home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Jumpstart / Multimedia Microsoft Jumpstart Version 1.1a (Microsoft).BIN / develpmt / source / sprites / debug.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-05  |  6.1 KB  |  277 lines

  1. /*
  2.     debug.c
  3.  
  4.     Functions to support a popup debug window
  5.  
  6. */
  7.  
  8. #include "global.h"
  9.  
  10. //
  11. // Constants
  12. //
  13.  
  14. #define MAXLISTLINES    200     // max list lines we keep
  15. #define IDC_LIST        1       // listbox id
  16.  
  17. //
  18. // local data
  19. //
  20.  
  21. HWND hwndDebug;
  22. HWND hwndDebugList;
  23.  
  24. #ifdef DEBUG
  25.  
  26. //
  27. // Set the current debug level
  28. //
  29.  
  30. void SetDebugLevel(int i)
  31. {
  32.     HMENU hMenu;
  33.     int m;
  34.  
  35.     hMenu = GetMenu(hwndMain); 
  36.     for (m=IDM_DEBUG0; m<=IDM_DEBUG4; m++) {
  37.         CheckMenuItem(hMenu, m, MF_UNCHECKED);
  38.     }
  39.     CheckMenuItem(hMenu, i + IDM_DEBUG0, MF_CHECKED);
  40.     __iDebugLevel = i;
  41. }
  42.  
  43. //
  44. // Show a message box with assertion failure info in it
  45. //
  46.  
  47. void __AssertMsg(LPSTR exp, LPSTR file, int line)
  48. {
  49.     char buf[256];
  50.     int i;
  51.  
  52.     wsprintf(buf, 
  53.              "Exp: %s\nFile: %s, Line %d",
  54.              (LPSTR)exp,
  55.              (LPSTR)file,
  56.              line);
  57.     i = MessageBox(hwndMain,
  58.                    buf,
  59.                    "Assertion failure", 
  60.                    MB_OK | MB_ICONEXCLAMATION);
  61. }
  62.  
  63. //
  64. // function to add a string to the end of the debug list
  65. //
  66.  
  67. void cdecl DbgOut(LPSTR lpFormat, ...) 
  68. {
  69.     int i;
  70.     char buf[256];
  71.  
  72.     //
  73.     // See if we have a debug window or not.
  74.     // If not then try to create one.
  75.     //
  76.  
  77.     if (!hwndDebug) {
  78.  
  79.         wsprintf(buf, "%s - Debug", (LPSTR)szAppName);
  80.         hwndDebug = CreateWindow("Debug",
  81.                             buf,
  82.                             WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
  83.                                 | WS_BORDER | WS_THICKFRAME | WS_MINIMIZEBOX,
  84.                             GetSystemMetrics(SM_CXSCREEN) * 3 / 4,
  85.                             GetSystemMetrics(SM_CYSCREEN) / 2,
  86.                             GetSystemMetrics(SM_CXSCREEN) / 4,
  87.                             GetSystemMetrics(SM_CYSCREEN) / 2,
  88.                             (HWND)NULL,
  89.                             (HMENU)NULL,
  90.                             hAppInstance,
  91.                             (LPSTR)NULL
  92.                             );
  93.  
  94.         if (!hwndDebug) return;
  95.     }
  96.  
  97.     //
  98.     // format the string
  99.     //
  100.  
  101.     wvsprintf(buf, lpFormat, (LPSTR)(&lpFormat+1));
  102.  
  103.     //
  104.     // stop the listbox repaints while we mess with it
  105.     //
  106.  
  107.     SendMessage(hwndDebugList, WM_SETREDRAW, (WPARAM) FALSE, (LPARAM) 0);
  108.  
  109.     //
  110.     // get the item count
  111.     //
  112.  
  113.     i = (int) SendMessage(hwndDebugList, LB_GETCOUNT, (WPARAM) 0, (LPARAM) 0);
  114.     if (i == LB_ERR) i = 0;
  115.  
  116.     //
  117.     // scrub a few if we have too many
  118.     //
  119.  
  120.     while (i >= MAXLISTLINES) {
  121.         SendMessage(hwndDebugList, LB_DELETESTRING, (WPARAM) 0, (LPARAM) 0);
  122.         i--;
  123.     }
  124.  
  125.     //
  126.     // add the new one on at the end and scroll it into view
  127.     //
  128.  
  129.     i = (int) SendMessage(hwndDebugList, LB_ADDSTRING, (WPARAM) 0, (LPARAM) (LPSTR) buf);
  130.     SendMessage(hwndDebugList, LB_SETCURSEL, (WPARAM) i, (LPARAM) 0);
  131.  
  132.     //
  133.     // enable the repaint now
  134.     //
  135.  
  136.     SendMessage(hwndDebugList, WM_SETREDRAW, (WPARAM) TRUE, (LPARAM) 0);
  137. }
  138.  
  139. //
  140. // Measure an item in our debug listbox
  141. //
  142.  
  143. static void MeasureDebugItem(HWND hWnd, LPMEASUREITEMSTRUCT lpMIS)
  144. {
  145.     TEXTMETRIC tm;
  146.     HDC hDC;
  147.  
  148.  
  149.     hDC = GetDC(hWnd);
  150.     GetTextMetrics(hDC, &tm);
  151.     ReleaseDC(hWnd, hDC);
  152.     lpMIS->itemHeight = tm.tmHeight;
  153. }
  154.  
  155. //
  156. // Display an item in our debug listbox
  157. //
  158.  
  159. void DrawDebugItem(HWND hWnd, LPDRAWITEMSTRUCT lpDI)
  160. {
  161.     HBRUSH hbrBkGnd;
  162.     RECT rc;
  163.     HDC hDC;
  164.     char buf[256];
  165.     
  166.     hDC = lpDI->hDC;
  167.     rc = lpDI->rcItem;
  168.  
  169.     switch (lpDI->itemAction) {
  170.  
  171.     case ODA_SELECT:
  172.     case ODA_DRAWENTIRE:
  173.  
  174.         //
  175.         // erase the rectangle
  176.         //
  177.  
  178.         hbrBkGnd = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
  179.         FillRect(hDC, &rc, hbrBkGnd);
  180.         DeleteObject(hbrBkGnd);
  181.  
  182.             //
  183.             // show the text in our standard font
  184.             //
  185.     
  186.             SetBkMode(hDC, TRANSPARENT);
  187.     
  188.             SendMessage(lpDI->hwndItem, 
  189.                         LB_GETTEXT, 
  190.                         lpDI->itemID, 
  191.                         (LPARAM)(LPSTR)buf);
  192.  
  193.             ExtTextOut(hDC, 
  194.                        rc.left+2, rc.top,
  195.                        ETO_CLIPPED,
  196.                        &rc, 
  197.                        buf,
  198.                        lstrlen(buf), 
  199.                        NULL);
  200.     
  201.         break;
  202.     
  203.     }
  204. }
  205.  
  206.  
  207. #endif // DEBUG
  208.  
  209. //
  210. // Window procedure for debug window
  211. //
  212.  
  213. LRESULT CALLBACK DebugWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  214. {
  215.     PAINTSTRUCT ps;
  216.  
  217.     switch(msg) {
  218.     case WM_CREATE:
  219.  
  220.         //
  221.         // Create the listbox 
  222.         //
  223.  
  224.         hwndDebugList = CreateWindow("Listbox",
  225.                             "",
  226.                             WS_CHILD | WS_VISIBLE | WS_VSCROLL
  227.                              | LBS_DISABLENOSCROLL
  228.                              | LBS_HASSTRINGS | LBS_OWNERDRAWFIXED
  229.                              | LBS_NOINTEGRALHEIGHT,
  230.                             0,
  231.                             0,
  232.                             0,
  233.                             0,
  234.                             hWnd,
  235.                             (HMENU)IDC_LIST,
  236.                             hAppInstance,
  237.                             (LPSTR)NULL
  238.                             );
  239.  
  240.         break;
  241.  
  242.     case WM_SIZE:
  243.         SetWindowPos(hwndDebugList,
  244.                      NULL,
  245.                      0, 0,
  246.                      LOWORD(lParam), HIWORD(lParam),
  247.                      SWP_NOZORDER);
  248.         break;
  249.  
  250.     case WM_SETFOCUS:
  251.         SetFocus(hwndDebugList);
  252.         break;
  253.  
  254.     case WM_MEASUREITEM:
  255.         MeasureDebugItem(hWnd, (LPMEASUREITEMSTRUCT)lParam);
  256.         return (LRESULT) TRUE;
  257.  
  258.     case WM_DRAWITEM:
  259.         DrawDebugItem(hWnd, (LPDRAWITEMSTRUCT) lParam);
  260.         break;
  261.  
  262.     case WM_PAINT:
  263.         BeginPaint(hWnd, &ps);
  264.         EndPaint(hWnd, &ps);
  265.         break;
  266.  
  267.     case WM_DESTROY:
  268.         hwndDebug = NULL;
  269.         break;
  270.  
  271.     default:
  272.         return DefWindowProc(hWnd, msg, wParam, lParam);
  273.         break;
  274.     }
  275.     return NULL;
  276. }
  277.